/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021.
 * Description: provides external functions for straightly obtaining
 * 		the CPU usage data of a thread
 * Author: chenzechuan
 * Create: 2021-4-25
 */

#include <linux/sched/signal.h>
#include <linux/sched/cputime.h>
#include <linux/sched/rtos_thread_time.h>
/*
 * provides an external interface for straightly obtaining the CPU usage
 * data(cutime, cstime, utime and stime) of a thread
 */
int thread_group_cputime_get(struct task_struct *p, u64 *ut, u64 *st,
			     u64 *cut, u64 *cst)
{
	unsigned long flags;
	struct signal_struct *sig = NULL;
	if (!p || !p->signal || !ut || !st || !cut || !cst)
		return -EINVAL;

	*cut = *cst = *ut = *st = 0;
	sig = p->signal;
	if (lock_task_sighand(p, &flags)) {
		*cut = sig->cutime;
		*cst = sig->cstime;
		thread_group_cputime_adjusted(p, ut, st);
		unlock_task_sighand(p, &flags);

		*ut = nsec_to_clock_t(*ut);
		*st = nsec_to_clock_t(*st);
		*cut = nsec_to_clock_t(*cut);
		*cst = nsec_to_clock_t(*cst);
		return 0;
	}
	return -ENOLCK;
}
EXPORT_SYMBOL_GPL(thread_group_cputime_get);
